package tt.dz.biz.service.rpc;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import javax.annotation.Resource;
import org.apache.avro.Protocol;
import org.apache.avro.Protocol.Message;
import org.apache.avro.generic.GenericRecord;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import tt.dz.biz.service.financial.MerchantService;
import tt.dz.common.DZStringUtil;
import tt.dz.common.DateUtil;
import tt.dz.common.DzErrorCode;
import tt.dz.common.DzErrorCode.CommonErrorCode;
import tt.dz.common.DzErrorCode.ExceptionErrorCode;
import tt.dz.common.RPCUtil;
import tt.dz.vo.MerchantVO;

@Service("rpcMerchantService")
public class RPCMerchantService
  implements RPCBaseService
{
  @Resource(name="merchantService")
  private MerchantService merchantService;
  
  public Object execute(Protocol.Message message, Object request, Protocol protocol)
  {
    String name = message.getName();
    Object reqJson = ((GenericRecord)request).get("req");
    String resJson = "";
    if ("queryMerchant".equals(name)) {
      resJson = queryIncrementMerchant(reqJson.toString());
    }
    return resJson;
  }
  
  public String queryIncrementMerchant(String reqJson)
  {
    JSONObject resObj = null;
    try
    {
      JSONObject reqObj = JSON.parseObject(reqJson);
      String startTime = reqObj.getString("startTime");
      String endTime = reqObj.getString("endTime");
      String providerType = reqObj.getString("providerType");
      SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
      if (StringUtils.isNotBlank(providerType)) {
        if (!DZStringUtil.verifyValueInEnums(providerType, new String[] { "0", "1", "2" }))
        {
          resObj = RPCUtil.generateResponseObject(Integer.valueOf(0), DzErrorCode.CommonErrorCode.PARAM_FORMAT_ERROR.value(), "providerType传入值错误！");
          
          return JSON.toJSONString(resObj);
        }
      }
      if ((StringUtils.isNotBlank(startTime)) && (!DateUtil.validateIsDate("yyyy-MM-dd", startTime)))
      {
        resObj = RPCUtil.generateResponseObject(Integer.valueOf(0), DzErrorCode.CommonErrorCode.PARAM_FORMAT_ERROR.value(), "startTime传入格式错误！");
        
        return JSON.toJSONString(resObj);
      }
      if ((StringUtils.isNotBlank(endTime)) && (!DateUtil.validateIsDate("yyyy-MM-dd", endTime)))
      {
        resObj = RPCUtil.generateResponseObject(Integer.valueOf(0), DzErrorCode.CommonErrorCode.PARAM_FORMAT_ERROR.value(), "endTime传入格式错误！");
        
        return JSON.toJSONString(resObj);
      }
      if (StringUtils.isBlank(startTime)) {
        startTime = dateFormat.format(DateUtil.getDBCurrentTime());
      }
      if (StringUtils.isBlank(endTime))
      {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(DateUtil.getDBCurrentTime());
        calendar.add(5, 1);
        endTime = dateFormat.format(calendar.getTime());
      }
      List<MerchantVO> merchantList = this.merchantService.queryIncrementMerchant(startTime, endTime, providerType);
      
      resObj = RPCUtil.generateResponseObject(Integer.valueOf(1), null, null);
      resObj.put("result", merchantList);
    }
    catch (JSONException e)
    {
      resObj = RPCUtil.generateResponseObject(Integer.valueOf(0), DzErrorCode.CommonErrorCode.FORMAT_ERROR.value(), DzErrorCode.CommonErrorCode.FORMAT_ERROR
        .getDesc());
    }
    catch (Exception e)
    {
      resObj = RPCUtil.generateResponseObject(Integer.valueOf(0), DzErrorCode.ExceptionErrorCode.COMMON_EXCEPTION.value(), "查询增量运营商异常,请稍后重试！");
    }
    return JSON.toJSONString(resObj);
  }
}
